/*
 * Copyright 2013 Insu Song. All rights reserved.
 * inssong@gmail.com  http://www.kopo.com  
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied.
 */
package com.kopo.droidlipokit;

import com.kopo.droidioiolib.Behavior;
import com.kopo.droidioiolib.ControlInterface;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

public class BehaviorTest extends Behavior {
	private SeekBar testv_pwm1Sb;
	private SeekBar testv_pwm2Sb;

	//private ToggleButton testv_toggleBtn;
	private ToggleButton testv_toggleBtn1;
	private ToggleButton testv_toggleBtn2;
	private TextView testv_txt;

	private int pwm1Dir = 0;
	private int pwm2Dir = 0;

	private int pwm1Duty = 0;
	private int pwm2Duty = 0;
	
	public BehaviorTest(View view) {
		super(view);
		// TODO Auto-generated constructor stub
		Log.d("kopo.log", "BehaviorTest created");
	}

	@Override
	public void setup() {
		Log.d("kopo.log", "BehaviorTest.setup");

		testv_pwm1Sb = (SeekBar) view.findViewById(R.id.testv_pwm1Sb);
		testv_pwm2Sb = (SeekBar) view.findViewById(R.id.testv_pwm2Sb);

		testv_toggleBtn1 = (ToggleButton) view.findViewById(R.id.testv_toggleBtn1);
		testv_toggleBtn2 = (ToggleButton) view.findViewById(R.id.testv_toggleBtn2);
		testv_txt = (TextView) view.findViewById(R.id.testv_txt);

		this.initialized = true;
	}

	@Override
	public void loop() {
		if( !initialized )
		{
			this.setup();
		}

		
		// Read buttons and update control device
		//-----------------
		pwm1Duty = testv_pwm1Sb.getProgress();  // must be 0-100
		pwm1Dir = testv_toggleBtn1.isChecked()?1:0;
		ControlInterface.me.setPwm(0, pwm1Duty, pwm1Dir);
		//Log.d("kopo.log", "BehaviorTest.loop: v=" + v + " dir1=" + pwm1Dir);

		//-----------------
		pwm2Duty = testv_pwm2Sb.getProgress();
		pwm2Dir =  testv_toggleBtn2.isChecked()?1:0;
		ControlInterface.me.setPwm(1, pwm2Duty, pwm2Dir);

		// Update UI if any
		if( MyControlDevice.currentActivity != null)
			MyControlDevice.currentActivity.runUI(); // invoke uiUpdate in UIThread		
	}

	@Override
	public void cleanup() {
	}

	@Override
	public void uiUpdate(Context context) {
		// Display textual information of the device
		if( testv_txt != null)
			testv_txt.setText("PWM1:  " + Float.toString(pwm1Duty));
	}

}
